package com.example.dynamicschedule.task;


import com.example.dynamicschedule.bean.CkFeedbackConfigTable;
import com.example.dynamicschedule.config.ScanningCleaningConfig;
import com.example.dynamicschedule.service.CkFeedBackConfigService;
import com.example.dynamicschedule.service.CustomerService;
import com.example.dynamicschedule.service.IntelligenceCallService;
import com.example.dynamicschedule.utils.FileDownloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Repository
public class DownLoadFileTask {

    @Autowired
    private FileDownloadUtil fileDownloadUtil;
    @Autowired
    private ScanningCleaningConfig scanningCleaningConfig;
    @Autowired
    private IntelligenceCallService intelligenceCallService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CkFeedBackConfigService ckFeedBackConfigService;
    static Logger log = Logger.getLogger(DownLoadFileTask.class);


    /**
     * 下载文件任务
     *
     */
    @Transactional
    public void DownLoadFileTask() throws Exception{
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr = simpleDateFormat.format(date);
        Map<String, CkFeedbackConfigTable> feedBackConfigMap = ckFeedBackConfigService.selectList();
        log.info("下载任务开始");
        log.info("--------智能客服下载oss服务上的json文件开始---------");
        try {
            String prefixDownJsonPath = scanningCleaningConfig.getPrefixDownJsonPath().replace("yyyyMMdd", dateStr);
            String intelligenceDownloadJsonPath = scanningCleaningConfig.getIntelligenceDownloadJsonPath();
            String localPath = intelligenceDownloadJsonPath + prefixDownJsonPath;
            File file = new File(intelligenceDownloadJsonPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            fileDownloadUtil.downLoad(scanningCleaningConfig.getPackageName()+"collection_online_loan_intelligence/"+prefixDownJsonPath,localPath);
            File localPathFile = new File(localPath);
            if (localPathFile.exists()) {
                intelligenceCallService.intelligenceCallService(localPath,feedBackConfigMap);
            } else {
                log.info("--------该地址:"+localPath+",无文件(或oss上无文件)---------");
            }
            log.info("--------智能客服下载oss服务上的json文件结束---------");
        } catch (Exception e) {
            log.error("--------智能客服下载oss服务上的json文件异常---------"+e.getMessage(), e);
        }
        log.info("--------人工客服下载oss服务上的json文件开始---------");
        try {
            /*String customerJsonFile = customerService.getCustomerJsonFile(feedBackConfigMap);
            log.info(customerJsonFile);*/
        } catch (Exception e) {
            log.error( "--------人工客服下载oss服务上的json文件异常---------"+e.getMessage(),e);

        }
        log.info("--------人工客服下载oss服务上的json文件结束---------");
    }
}
